@echo off

REM
REM *******************************************************************
REM
REM It is a script to commit code in Windows platform to svn server instead of running 
REM 'svn commit' directly.
REM This script will autoincrement the number in 'buildnum.txt' and CppflibBuildNum.h
REM before commiting code and will revert back the number to its original if commit failed
REM
REM Usage: just run this script directly
REM
REM > win_svn_commit.bat
REM 
REM *******************************************************************
REM

setlocal

REM
REM Increment build number
REM
set /p cppflibbuildnum= < buildnum.txt
set /a cppflibbuildnum=%cppflibbuildnum% + 1
echo Build number ready to commit -- %cppflibbuildnum%
echo %cppflibbuildnum% > buildnum.txt

call:Update_CppflibBuildNum %cppflibbuildnum%


REM
REM Get original revision
REM
svnversion > temprev1.txt
set /p cppflib_rev1= < temprev1.txt
del temprev1.txt

REM
REM Do actual commit
REM
svn commit

REM
REM Get revision after commit
REM
svnversion > temprev2.txt
set /p cppflib_rev2= < temprev2.txt
del temprev2.txt

echo ===================================================================
echo before commit = %cppflib_rev1%, after commit = %cppflib_rev2%
echo ===================================================================

if "%cppflib_rev1%" == "%cppflib_rev2%" goto no_change
echo Commit sucessfully
echo:

goto end

:no_change
echo Commit failed ...

REM
REM Revert changes
REM
echo Reverting changes ...
svn revert buildnum.txt
svn revert cppflib\common\CppflibBuildNum.h

:end

endlocal

goto:eof



REM
REM A function to update CppflibBuildNum.h
REM
:Update_CppflibBuildNum
(
echo /**
echo  *  @file CppflibBuildNum.h
echo  *
echo  *  Don't edit this file manually as it is generated by a script
echo  */
echo #ifndef _CPPFLIBBUILDNUM_H_
echo #define _CPPFLIBBUILDNUM_H_
echo:
echo // this number is incremented by 1 for each commit
echo #define FLIB_BUILD   %~1
echo:
echo #endif
echo:
) > cppflib\common\CppflibBuildNum.h
goto:eof
